package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysDictDataService;
import org.asiainfo.system.domain.vo.SysDictDataVo;
import org.asiainfo.system.domain.bo.SysDictDataBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 字典数据表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dictData")
public class SysDictDataController extends BaseController {

    private final SysDictDataService sysDictDataService;

    /**
     * 查询字典数据列表 分页查询
     *
     * @param sysDictDataBo 筛选条件
     * @param pageQuery     分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictDataVo> list(SysDictDataBo sysDictDataBo, PageQuery pageQuery) {
        return sysDictDataService.queryPageList(sysDictDataBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dictCode 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping("/{dictCode}")
    public R<SysDictDataVo> getInfo(@NotNull(message = "字典编码不能为空") @PathVariable("dictCode") Long dictCode) {
        return R.ok(sysDictDataService.queryById(dictCode));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dictType 主键
     * @return 单条数据
     */
    @GetMapping("/type/{dictType}")
    public R<List<SysDictDataVo>> getInfo(@NotNull(message = "字典类型不能为空") @PathVariable("dictType") String dictType) {
        return R.ok(sysDictDataService.selectDictDataByType(dictType));
    }

    /**
     * 新增数据
     *
     * @param sysDictDataBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典数据表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysDictDataBo sysDictDataBo) {
        sysDictDataService.insertByBo(sysDictDataBo);
        return R.ok();
    }

    /**
     * 编辑数据
     *
     * @param sysDictDataBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典数据表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDictDataBo sysDictDataBo) {
        sysDictDataService.updateByBo(sysDictDataBo);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param dictCodes 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典数据表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R<Void> remove(@NotNull(message = "字典编码不能为空") @PathVariable Long[] dictCodes) {
        return toAjax(sysDictDataService.deleteWithValidByIds(List.of(dictCodes)));
    }
}

